package weaver.interfaces.unine;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName FixedAssetsAction
 * @Description TODO
 * @Author ocean
 * @Date 2022/8/29 16:47
 * @Version 1.0
 */
public class SupplierAddAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {

        SupplierUtil supplierUtil = new SupplierUtil();
        String requestid = requestInfo.getRequestid();
        int formId = requestInfo.getRequestManager().getFormid();
        RecordSet rs = new RecordSet();
        writeLog( ">>>>>>SupplierAddAction>>>>>" + requestid );
        try {
            Map mainTableDataMap = new HashMap();
            //取主表数据
            // 获取表单主字段信息
            Property[] properties = requestInfo.getMainTableInfo().getProperty();
            for (Property property : properties) {
                // 主字段名称
                String name = property.getName().toLowerCase();
                // 主字段对应的值
                String value = Util.null2String( property.getValue() );
                mainTableDataMap.put( name, value );
            }
            writeLog( ">>>>>>mainTableDataMap>>>>>" + mainTableDataMap.toString() );
            String gfszzgs = Util.null2String( (String) mainTableDataMap.get( "gfszzgs" ) );
            String orgCode = "999";
            String createdBy = supplierUtil.getWorkCode( Util.null2String( (String) mainTableDataMap.get( "sqr" ) ) );
            String name = Util.null2String( (String) mainTableDataMap.get( "gysqc" ) );
            String contactMan = Util.null2String( (String) mainTableDataMap.get( "gflxr" ) );
            String bankName = Util.null2String( (String) mainTableDataMap.get( "gysyxqc" ) );
            String toOrgCode = supplierUtil.getCompanyCode( gfszzgs );
            String invType = supplierUtil.getSelectValue( formId, "kplx", Util.null2String( (String) mainTableDataMap.get( "kplx" ) ) );
            String contactPhone = Util.null2String( (String) mainTableDataMap.get( "gflxdh" ) );
            String bankAccountName = Util.null2String( (String) mainTableDataMap.get( "gyszhkh" ) );
            String setMethod = supplierUtil.getSelectValue( formId, "jsfs", Util.null2String( (String) mainTableDataMap.get( "jsfs" ) ) );
            String taxRate = supplierUtil.getSelectValue( formId, "sl", Util.null2String( (String) mainTableDataMap.get( "sl" ) ) );

            Double taxRateDouble = Double.parseDouble( taxRate.replace( "%", "" ) ) * 0.01;

            String stateTaxNo = Util.null2String( (String) mainTableDataMap.get( "tyshxydm" ) );
            String jyAddress = Util.null2String( (String) mainTableDataMap.get( "gysdz" ) );
            String zcAddress = Util.null2String( (String) mainTableDataMap.get( "gyszcdz" ) );
            String contactMail = Util.null2String( (String) mainTableDataMap.get( "gfyx" ) );
            String suppType = supplierUtil.getSelectValue( formId, "gyslx", Util.null2String( (String) mainTableDataMap.get( "gyslx" ) ) );
            String isMISC = supplierUtil.getSelectValue( formId, "sfwlsgys", Util.null2String( (String) mainTableDataMap.get( "sfwlsgys" ) ) );
            String accPeriod = supplierUtil.getSelectValue( formId, "zq", Util.null2String( (String) mainTableDataMap.get( "zq" ) ) );
            String confirmCode = Util.null2String( (String) mainTableDataMap.get( "ghdm2" ) );
            String sarea = "";
            String sprovince = supplierUtil.getProvince( Util.null2String( (String) mainTableDataMap.get( "gfszsf" ) ) );
            String scity = supplierUtil.getCity( Util.null2String( (String) mainTableDataMap.get( "gfszcs" ) ) );
            String scounty = supplierUtil.getCityTwo( Util.null2String( (String) mainTableDataMap.get( "gfszqx" ) ) );

            String action = "ICreateSupplySV.svc/do";
            String url = getPropValue( "u9yk", "url" ) + action;
            JSONObject contextMap = supplierUtil.getContextInfo();
            Map<String, Object> inputMap = new HashMap<String, Object>();
            inputMap.put( "context", contextMap );
            inputMap.put( "orgCode", orgCode );
            inputMap.put( "createdBy", createdBy );
            inputMap.put( "name", name );
            inputMap.put( "contactMan", contactMan );
            inputMap.put( "bankName", bankName );
            inputMap.put( "toOrgCode", toOrgCode );
            inputMap.put( "invType", invType );
            inputMap.put( "contactPhone", contactPhone );
            inputMap.put( "bankAccountName", bankAccountName );
            inputMap.put( "setMethod", setMethod );
            inputMap.put( "taxRate", taxRateDouble );
            inputMap.put( "stateTaxNo", stateTaxNo );
            inputMap.put( "jyAddress", jyAddress );
            inputMap.put( "zcAddress", zcAddress );
            inputMap.put( "contactMail", contactMail );
            inputMap.put( "suppType", suppType );
            inputMap.put( "isMISC", isMISC );
            inputMap.put( "accPeriod", accPeriod );
            inputMap.put( "confirmCode", confirmCode );
            inputMap.put( "sarea", sarea );
            inputMap.put( "sprovince", sprovince );
            inputMap.put( "scity", scity );
            inputMap.put( "scounty", scounty );
            String back = supplierUtil.doSupplier( url, JSON.toJSONString( inputMap ) );
            writeLog( "inputMap_JSON=" + JSON.toJSONString( inputMap ) );
            writeLog( "back=" + back );

            Map<String, Object> jsonMap= JSON.parseObject(back,HashMap.class );
            Map<String, Object> backJson= JSON.parseObject(Util.null2String(jsonMap.get( "d" )),HashMap.class );
            writeLog( "backJson=" + backJson );
            String status = Util.null2String(backJson.get( "Status" ));
            writeLog( "status=" + status );
            String msg = Util.null2String( backJson.get( "Msg" ));
            writeLog( "msg=" + msg );
            if ("1".equals( status )) {
                String code = Util.null2String(backJson.get( "Code" ));
                writeLog( "code=" + code );
                rs.execute( "update uf_gysxx set u9gysbm='" + code + "' where lc=" + requestid );
                return Action.SUCCESS;
            } else {
                requestInfo.getRequestManager().setMessageid( "111111" );
                requestInfo.getRequestManager().setMessagecontent( msg );
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid( "111111" );
            requestInfo.getRequestManager().setMessagecontent( "Action接口异常！" );
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
